package pachet.doi;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Bundle;
import android.widget.TextView;

public class BatteryInfo extends Activity {
	private TextView mStatusTxt;
	private TextView mcontentTxt;
	private TextView mScaleTxt;
	private TextView mVoltageTxt;
	private TextView mHealthTxt;
	private TextView mTemperatureTxt;
	private TextView mTechnologyTxt;
	private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context arg0, Intent intent) {
			// TODO Auto-generated method stub
			int plugType = intent.getIntExtra("plugged", 0);
			int level = intent.getIntExtra("level", 0);
			mcontentTxt.setText(String.valueOf(level) + "%");
			int mScale = intent.getIntExtra("scale", 0);
			mScaleTxt.setText(String.valueOf(mScale) + "%");

			mVoltageTxt.setText("" + intent.getIntExtra("voltage", 0) + " "
					+ getString(R.string.battery_info_voltage_units));
			mTemperatureTxt.setText(""
					+ tenthsToFixedString(intent.getIntExtra("temperature", 0))
					+ getString(R.string.battery_info_temperature_units));
			mTechnologyTxt.setText("" + intent.getStringExtra("technology"));

			int status = intent.getIntExtra("status",
					BatteryManager.BATTERY_STATUS_UNKNOWN);
			String statusString;
			if (status == BatteryManager.BATTERY_STATUS_CHARGING) {
				statusString = getString(R.string.battery_info_status_charging);
				if (plugType > 0) {
					statusString = statusString
							+ " "
							+ getString((plugType == BatteryManager.BATTERY_PLUGGED_AC) ? R.string.battery_info_status_charging_ac
									: R.string.battery_info_status_charging_usb);
				}
			} else if (status == BatteryManager.BATTERY_STATUS_DISCHARGING) {
				statusString = getString(R.string.battery_info_status_discharging);
			} else if (status == BatteryManager.BATTERY_STATUS_NOT_CHARGING) {
				statusString = getString(R.string.battery_info_status_not_charging);
			} else if (status == BatteryManager.BATTERY_STATUS_FULL) {
				statusString = getString(R.string.battery_info_status_full);
			} else {
				statusString = getString(R.string.battery_info_status_unknown);
			}
			mStatusTxt.setText(statusString);

			int health = intent.getIntExtra("health",
					BatteryManager.BATTERY_HEALTH_UNKNOWN);
			String healthString;
			if (health == BatteryManager.BATTERY_HEALTH_GOOD) {
				healthString = getString(R.string.battery_info_health_good);
			} else if (health == BatteryManager.BATTERY_HEALTH_OVERHEAT) {
				healthString = getString(R.string.battery_info_health_overheat);
			} else if (health == BatteryManager.BATTERY_HEALTH_DEAD) {
				healthString = getString(R.string.battery_info_health_dead);
			} else if (health == BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE) {
				healthString = getString(R.string.battery_info_health_over_voltage);
			} else if (health == BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE) {
				healthString = getString(R.string.battery_info_health_unspecified_failure);
			} else {
				healthString = getString(R.string.battery_info_health_unknown);
			}
			mHealthTxt.setText(healthString);
		}
	};

	private final String tenthsToFixedString(int x) {
		int tens = x / 10;
		return new String("" + tens + "." + (x - 10 * tens));
	}

	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.battery);
		mcontentTxt = (TextView) this.findViewById(R.id.monospaceTxt);
		mScaleTxt = (TextView) this.findViewById(R.id.mScaleTxt);
		mVoltageTxt = (TextView) this.findViewById(R.id.mVoltageTxt);
		mTemperatureTxt = (TextView) this.findViewById(R.id.mTemperatureTxt);
		mTechnologyTxt = (TextView) findViewById(R.id.mTechnologyTxt);
		mStatusTxt = (TextView) findViewById(R.id.mStatusTxt);
		mHealthTxt = (TextView) findViewById(R.id.mHealthTxt);
		this.registerReceiver(this.mBatInfoReceiver, new IntentFilter(
				Intent.ACTION_BATTERY_CHANGED));
	}
}